<?php
/**
 * best_sellers sidebox - displays selected number of (usu top ten) best selling products
 *
 * @package templateSystem
 * @copyright Copyright 2003-2005 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: best_sellers.php 2718 2005-12-28 06:42:39Z drbyte $
 */

// test if box should display
  $show_best_sellers= false;

  if (isset($_GET['products_id'])) {
    if (isset($_SESSION['customer_id'])) {
      $check_query = "select count(*) as count
                      from " . TABLE_CUSTOMERS_INFO . "
                      where customers_info_id = '" . (int)$_SESSION['customer_id'] . "'
                      and global_product_notifications = '1'";

      $check = $db->Execute($check_query);

      if ($check->fields['count'] > 0) {
        $show_best_sellers= true;
      }
    }
  } else {
    $show_best_sellers= true;
  }

  if ($show_best_sellers == true) {
    if (isset($current_category_id) && ($current_category_id > 0)) {
      $best_sellers_query = "select distinct p.products_id, pd.products_name, p.products_ordered,p.products_image
                             from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, "
                                    . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c
                             where p.products_status = '1'
                             and p.products_ordered > 0
                             and p.products_id = pd.products_id
                             and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                             and p.products_id = p2c.products_id
                             and p2c.categories_id = c.categories_id
                             and '" . (int)$current_category_id . "' in (c.categories_id, c.parent_id)
                             order by p.products_ordered desc, pd.products_name
                             limit " . MAX_DISPLAY_BESTSELLERS;

      $best_sellers = $db->Execute($best_sellers_query);

    } else {
      $best_sellers_query = "select distinct p.products_id, pd.products_name, p.products_ordered,p.products_image
                             from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd
                             where p.products_status = '1'
                             and p.products_ordered > 0
                             and p.products_id = pd.products_id
                             and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                             order by p.products_ordered desc, pd.products_name
                             limit " . MAX_DISPLAY_BESTSELLERS;

      $best_sellers = $db->Execute($best_sellers_query);
    }

  }
?>

<table width="778" cellspacing="0" cellpadding="0" border="0">
    <tbody>
        <tr>
            <td><?php echo zen_image(DIR_WS_TEMPLATE_IMAGES . 'tseller.gif'); ?></td>
        </tr>
        <tr>
            <td class="catTslBox">
                <table width="100%" cellspacing="0" cellpadding="0" border="0">
                    <tbody>
 <?php

 
$i=1;
      while (!$best_sellers->EOF) {
      	if($i%4==1){ echo '<tr>';}
      	
 echo '<td width="175" valign="top" style="border-bottom: 1px solid rgb(208, 208, 208); padding-bottom: 9px;">
                                <div style="width: 175px; height: 257px;overflow:hidden">
                                    <div class="catTslImg">
                                    <a href="' . zen_href_link(zen_get_info_page($best_sellers->fields['products_id']), 'products_id=' . $best_sellers->fields['products_id']) . '">'.zen_get_products_image($best_sellers->fields['products_id'],100,100).'</a>
                                    </div>
                                    <div class="catTslDesc">
                                    '.$best_sellers->fields['products_name'].'
                                    </div>
                                    <div class="catTslPrice">
                   '.zen_get_products_display_price($best_sellers->fields['products_id']).'
                                    </div>
                                    <div class="catTslAtc">
                            <a href="'.zen_href_link(FILENAME_PRODUCTS_ALL.'&action=buy_now&products_id='. $best_sellers->fields['products_id']) . '">'
 . zen_image_button(BUTTON_IMAGE_ADD_TO_CART, BUTTON_IN_CART_ALT).' </a></div>
                                </div>
                            </td>';
        

        
        $best_sellers->MoveNext();
        
	    if($best_sellers->EOF and ($i%4!=0)){
			echo '</tr>';
		}
		if($i%4==0){ echo '</tr>';}
		$i++;
	      }

  
   
 ?>      
                    </tbody>
                </table>
            </td>
        </tr>
        <tr>
            <td valign="bottom"><?php echo zen_image(DIR_WS_TEMPLATE_IMAGES . 'comm_bottom.gif'); ?></td>
        </tr>
    </tbody>
</table>